package org.light.verb;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Field;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.domain.Var;
import org.light.easyui.EasyUIPositions;
import org.light.generator.NamedStatementGenerator;
import org.light.generator.NamedStatementListGenerator;
import org.light.utils.InterVarUtil;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class FindById extends Verb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod(){
		try {
			Method method = new Method();
			method.setStandardName("find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
			method.setReturnType(new Type(this.domain.getCapFirstDomainNameWithSuffix(),this.domain.getPackageToken()));
			method.addAdditionalImport("java.sql.Connection");
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport("java.util.ArrayList");
			method.addAdditionalImport("java.sql.PreparedStatement");
			method.addAdditionalImport("java.sql.ResultSet");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+ this.domain.getDomainSuffix() +"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoSuffix()+"."+this.domain.getStandardName()+"Dao");
			if (this.domain.containsDateTime()) {
				method.addAdditionalImport(this.domain.getPackageToken()+".utils.DateUtil");
			}
			method.addSignature(new Signature(1,"connection",new Type("Connection","java.sql"),"java.sql"));
			method.addSignature(new Signature(2,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getRawType()));
			method.setThrowException(true);
			
			List<Writeable> list = new ArrayList<Writeable>();
			list.add(NamedStatementGenerator.getTryHead(1000L,2));
			list.add(NamedStatementListGenerator.generateFindByIdStatementListWithDeniedFields(2000L,3,this.domain,this.deniedFields));
			list.add(NamedStatementListGenerator.generateCatchExceptionPrintStackReturnNullFooter(3000L,2));
			method.setMethodStatementList(WriteableUtil.merge(list));
			return method;
		} catch (Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public Method generateDaoMethodDefinition() {
		Method method = new Method();
		method.setStandardName("find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
		method.setReturnType(new Type(this.domain.getCapFirstDomainNameWithSuffix(),this.domain.getPackageToken()));
		method.addAdditionalImport("java.sql.Connection");
		method.addAdditionalImport("java.util.List");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
		method.addSignature(new Signature(1,"connection",new Type("Connection","java.sql"),"java.sql"));
		method.addSignature(new Signature(2,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getRawType()));
		method.setThrowException(true);
		return method;
	}

	@Override
	public Method generateServiceMethodDefinition() {
		Method method = new Method();
		method.setStandardName("find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
		method.setReturnType(new Type(this.domain.getCapFirstDomainNameWithSuffix(),this.domain.getPackageToken()));
		method.addAdditionalImport("java.util.List");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
		method.addSignature(new Signature(1,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getRawType()));
		method.setThrowException(true);
		
		return method;
	}

	@Override
	public Method generateServiceImplMethod() {
		Method method = new Method();
		method.setStandardName("find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
		method.setReturnType(new Type(this.domain.getCapFirstDomainNameWithSuffix(),this.domain.getPackageToken()));
		method.addAdditionalImport("java.sql.Connection");
		method.addAdditionalImport("java.util.List");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.domain.getPackageToken()+".database.DBConf");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoSuffix()+"."+this.domain.getStandardName()+"Dao");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoimplSuffix()+"."+this.domain.getStandardName()+"DaoImpl");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
		method.addSignature(new Signature(1,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getRawType()));
		method.setThrowException(true);
		
		Method daomethod = this.generateDaoMethodDefinition();
		
		List<Writeable> list = new ArrayList<Writeable>();
		list.add(NamedStatementListGenerator.generateServiceImplReturnDomain(1000L, 2, InterVarUtil.DB.connection, InterVarUtil.DB.dbconf, this.domain, InterVarUtil.DB.dao, daomethod));
		method.setMethodStatementList(WriteableUtil.merge(list));

		return method;
	}
	
	public FindById(){
		super();
		if (this.domain != null) this.setVerbName("Find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
		else this.setVerbName("FindById");
	}
	
	public FindById(Domain domain){
		super();
		this.domain = domain;
		this.setVerbName("Find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
	}

	public FindById(Domain domain, Set<Field> deniedFields){
		super();
		this.domain = domain;
		this.deniedFields = deniedFields;
		this.dbType = domain.getDbType();
		this.setVerbName("Find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
	}

	@Override
	public Method generateControllerMethod() {
		Method method = new Method();
		method.setStandardName("processRequest");
		method.setReturnType(new Type("void"));
		method.setThrowException(true);
		List<String> list = new ArrayList<String>();
		list.add("ServletException");
		list.add("IOException");
		method.setIsprotected(true);
		method.setOtherExceptions(list);
		method.addSignature(new Signature(1,"request",new Type("HttpServletRequest","javax.servlet.http")));
		method.addSignature(new Signature(2,"response",new Type("HttpServletResponse","javax.servlet.http")));
		method.addAdditionalImport("java.io.IOException");
		method.addAdditionalImport("java.io.PrintWriter");
		method.addAdditionalImport("javax.servlet.ServletException");
		method.addAdditionalImport("javax.servlet.http.HttpServlet");
		method.addAdditionalImport("javax.servlet.http.HttpServletRequest");
		method.addAdditionalImport("javax.servlet.http.HttpServletResponse");
		method.addAdditionalImport("java.util.Map");
		method.addAdditionalImport("java.util.TreeMap");
		method.addAdditionalImport("net.sf.json.JSONObject");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceimplSuffix()+"."+this.domain.getStandardName()+"ServiceImpl");
		method.addAdditionalImport("net.sf.json.JsonConfig");
		method.addAdditionalImport("java.util.Date");
		method.addAdditionalImport(this.domain.getPackageToken()+".utils.JsonDateValueProcessor");
		method.addAdditionalImport(this.domain.getPackageToken()+".utils.JsonImageValueProcessor");
		
		List<Writeable> wlist = new ArrayList<Writeable>();
		Var service = new Var("service", new Type(this.domain.getStandardName()+"Service",this.domain.getPackageToken()));
		Var resultMap = new Var("result", new Type("TreeMap<String,Object>","java.util"));
		Var domainVar = new Var(this.domain.getLowerFirstDomainName(), new Type(this.domain.getCapFirstDomainNameWithSuffix(),this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix()));
		Method serviceMethod = this.generateServiceMethodDefinition();
		wlist.add(NamedStatementGenerator.getFacadeSetContentType(1000L, 2, InterVarUtil.Servlet.response, InterVarUtil.SimpleJEE.UTF8.getVarName()));
		wlist.add(NamedStatementGenerator.getJsonResultMap(2000L, 2, resultMap));
		wlist.add(NamedStatementGenerator.getControllerPrintWriterOut(3000L, 2, InterVarUtil.Servlet.response, InterVarUtil.Servlet.out));
		wlist.add(NamedStatementGenerator.getTryHead(4000L,2));
		wlist.add(NamedStatementGenerator.getSetDomainIdFromRequest(5000L, 3, this.domain, InterVarUtil.Servlet.request));
		wlist.add(new Statement(6000L,3,service.generateTypeVarString() + " = new " + service.getVarType().getTypeName()+"Impl();"));
		wlist.add(new Statement(7000L,3, domainVar.generateTypeVarString() + " = " + service.getVarName()+"."+serviceMethod.generateStandardCallString()+";"));
		wlist.add(NamedStatementListGenerator.getPutJsonResultMapWithSuccessAndDomainVar(8000L, 3, resultMap,domainVar));
		long serial = 8000L;
		wlist.add(new Statement(serial+1000L,3,"JsonConfig config = new JsonConfig();"));
		wlist.add(new Statement(serial+2000L,3,"JsonDateValueProcessor jsonDateValueProcessor = new JsonDateValueProcessor();"));
		wlist.add(new Statement(serial+3000L,3,"config.registerJsonValueProcessor(Date.class, jsonDateValueProcessor);"));
		wlist.add(new Statement(serial+4000L,3,"JsonImageValueProcessor jsonImageValueProcessor = new JsonImageValueProcessor();"));
		wlist.add(new Statement(serial+5000L,3,"config.registerJsonValueProcessor(byte [].class, jsonImageValueProcessor);"));
		wlist.add(NamedStatementGenerator.getEncodeMapToJsonResultMapWithConfig(serial+9000L, 3, resultMap,InterVarUtil.Servlet.out));
		wlist.add(NamedStatementListGenerator.generateCatchExceptionPrintStackPrintJsonMapFinallyCloseOutFooter(serial+10000L, 2, InterVarUtil.Servlet.response,resultMap, InterVarUtil.Servlet.out));
		method.setMethodStatementList(WriteableUtil.merge(wlist));
		
		return method;
	}

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;
	}
	
	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName(StringUtil.lowerFirst(this.getVerbName()));
			method.setReturnType(
					new Type(this.domain.getCapFirstDomainNameWithSuffix(), this.domain.getPackageToken()));
			method.addSignature(new Signature(1, this.domain.getDomainId().getFieldName(),
					this.domain.getDomainId().getClassType()));
			method.setThrowException(true);

			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(2000L,2,"for ("+this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+" : db) {"));
			sList.add(new Statement(3000L,3,"if ("+this.domain.getLowerFirstDomainName()+".get"+this.domain.getDomainId().getCapFirstFieldName()+"().equals("+this.domain.getDomainId().getLowerFirstFieldName()+")) return "+this.domain.getLowerFirstDomainName()+";"));
			sList.add(new Statement(4000L,2,"}"));
			sList.add(new Statement(5000L,2,"return null;"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public Method generateDummyServiceImplMethod() throws Exception {
		Method method = new Method();
		method.setStandardName("find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
		method.setReturnType(new Type(this.domain.getCapFirstDomainNameWithSuffix(),this.domain.getPackageToken()));
		method.addAdditionalImport("java.util.List");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoSuffix()+"."+this.domain.getStandardName()+"Dao");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDaoimplSuffix()+"."+this.domain.getStandardName()+"DummyDaoImpl");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
		method.addSignature(new Signature(1,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getRawType()));
		method.setThrowException(true);
		
		Method daomethod = this.generateDummyDaoMethodDefinition();		
		List<Writeable> list = new ArrayList<Writeable>();
		list.add(NamedStatementListGenerator.generateDummyServiceImplReturnDomain(1000L, 2, this.domain, InterVarUtil.DB.dao, daomethod));
		method.setMethodStatementList(WriteableUtil.merge(list));

		return method;
	}

	@Override
	public Method generateDummyDaoMethodDefinition() throws Exception {
		Method method = new Method();
		method.setStandardName("find"+this.domain.getStandardName()+"By"+this.domain.getDomainId().getCapFirstFieldName());
		method.setReturnType(new Type(this.domain.getCapFirstDomainNameWithSuffix(),this.domain.getPackageToken()));
		method.addAdditionalImport("java.util.List");
		method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
		method.addSignature(new Signature(1,this.domain.getDomainId().getFieldName(),this.domain.getDomainId().getRawType()));
		method.setThrowException(true);
		return method;
	}
}
